// pages/index/index.js.js
import api from "../../utils/api.js"
import {adddata,getban1,getban2} from "../../utils/urls.js"
Page({
  data: {
    //////*****表单数据实现双向绑定*****///// 
    name:null,
    date: "请输入出生年月",
    age:null,
    site:"",
    ipone:null,
    sex:1,
    profession:"",  ///职业
    //index2:"",//医师证certificate
    certificate:[],///拥有的医师证
    education:"",////学历
    specialty:"",//专业
    doctordate:"选择从医时间：",
    index1:null,  ////班次时间下标 
    classes:[],
    zongjiao:"",
    zhengzhi:"",
    techang:"",
    hunf:false,
    chuchai:false,
    bctime:"班次时间：",
    groupnumber:"",
    jishutext:"",
    classesdata:[], //全部班次数据
     
  ///////////////***********/////////////////
    docterlist:[], //医师证数据
    classeslist:[], ///班次数据
   
    

    modalName:"",
    
  },
  bindPickerChange(e){
    
    this.setData({
      index1:e.detail.value
    })
    //console.log(this.data.index1)
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getbandata();
    this.getdocterata();
  },
  clickti(e){
    console.log(e.currentTarget.dataset.bc)
    this.setData({
      index1:e.currentTarget.dataset.bc
    })
  },
  yanzheng(){
   let a=1
    if(this.data.name==null ){
      a="请输入姓名"
    }
   else if(this.data.date=="请输入出生年月"){
      a="请输入出生年月"
    }
   else if(this.data.age==null){
      a="请输入年龄"
    }
    else if(this.data.site==""){
      a="请输入地点"
    }
    else if(this.data.ipone==null){
      a="请输入手机号"
    }
    else if(this.data.profession==""){
      a="请输入职业"
    }
    else if(this.data.age<0||this.data.age>120||isNaN(this.data.age)){
      a="年龄错误"
    }else if(this.data.certificate.length==0){
      a="请输入医师证"
    }else if(this.data.classes.length==0){
      a="请选择现在班次"
    }
    else {
      if(this.data.doctordate=="选择从医时间："){
        this.setData({
          doctordate:"",
        })
      }
     
    }
    return a;
  },
/////提交
tijiao(){
  
//验证
  let yan=this.yanzheng();
  if(yan!=1){
    wx.showToast({
      title: yan,
      icon: 'error',
      duration: 2000
    })
    return 0;
  }
  console.log(this.data.date);
 
  let d=this.data
  api.get(adddata,
    {
      name:this.data.name,
      date:this.data.date,
      age:this.data.age,
      site:this.data.site,
      ipone:this.data.ipone,
      sex:this.data.sex,
      profession:this.data.profession,
      certificate:this.data.certificate.toString(),
      education:this.data.education,
      specialty:this.data.specialty,
      doctordate:this.data.doctordate,
      classes:this.data.classes.toString(),
      
      zongjiao:d.zongjiao,
      zhengzhi:d.zhengzhi,
      techang:d.techang,
      hunf:d.hunf,
      chuchai:d.chuchai,
     
      
      jishutext:d.jishutext,
      classesdata:d.classesdata,
    },
    ).then(res=>{
      if (res=="ok"){
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        }) 
       wx.switchTab({
         url: '/pages/rck/rck',
       })
      }
        else{
          wx.showToast({
            title: '数据格式有误',
            icon: 'error',
            duration: 2000
          })
        }
  })
 },
//从医时间
doctorChange(e){
  this.setData({
    doctordate:e.detail.value
  })
},
bctimeCh(e){
  console.log(e.detail.value)
  let in1=this.data.index1
  let datalist=this.data.classesdata
  datalist[in1].bctime=e.detail.value
  this.setData({
    classesdata:datalist
  })
  console.log(this.data.classesdata)
},
//性别
sexbutton(e){
  console.log(e.detail.value)
 this.setData({
   sex:e.detail.value
 })
},
///有证医师改变
zhengtf(e){
  this.setData({
    certificate: e.detail.value
  })
},

  ///出生年月改变
  DateChange(e){
    let date=new Date();
   let  presentyear=date.getFullYear();

    let cdate=e.detail.value;
    let datey=cdate.split("-")[0];
    
    this.setData({
      age:presentyear-datey
    })
    this.setData({
      date: e.detail.value
    })
  },

  //获取现在班次初始数据
  getbandata(){
   wx.showLoading({
     title: '加载中',
   })
  api.get(getban1).then(res=>{
    let d=[];
    for (let i=0; i<res.length;i++){
      d[i]={value:res[i],name:res[i],checked: false,
       hot: false,
      }}
       this.setData({
         "classeslist":d
       })
       wx.hideLoading()
  })
  //  wx.request({
  //    url: getApp().globalData.url+'getban/1',
  //    header:getApp().globalData.header,
  //    success:res=>{
  //   //   console.log(res.data)

  //   let d=[];
  //   for (let i=0; i<res.data.length;i++){
  //     d[i]={value:res.data[i],name:res.data[i],checked: false,
  //      hot: false,
  //     }}
  //      this.setData({
  //        "classeslist":d
  //      })
  //      wx.hideLoading()
//}
 //  })
  },

  getdocterata(){
    wx.showLoading({
      title: '加载中',
    })

    api.get(getban2).then(res=>{
      let d=[];
      let c=[];
      for (let i=0; i<res.length;i++){
      d[i]={value:res[i],name:res[i],checked: false,
       hot: false,}
      }
     
      
         this.setData({
           "docterlist":d
         })
       
         wx.hideLoading()
    })
    // wx.request({

    //   url: getApp().globalData.url+'getban/2',
    //   header:getApp().globalData.header,
    //   success:res=>{
    //  //   console.log(res.data)
    //  let d=[];
    //  let c=[];
    //  for (let i=0; i<res.data.length;i++){
    //  d[i]={value:res.data[i],name:res.data[i],checked: false,
    //   hot: false,}
    //  }
    
     
    //     this.setData({
    //       "docterlist":d
    //     })
      
    //     wx.hideLoading()
  //    }
    //})
   },
  // 医师证多选窗口
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  //班次
  showModal1(e) {
   
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  //取消
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  hideModal1(e) {
    this.setData({
      classesdata:[]
    })
    let d=[]
    let datal=this.data.classes
    for(let i=0;i<datal.length;i++){
     d[i]={
       name:datal[i],
       bctime:"加入时间",
       groupnumber:"",
     }
    }
    console.log(d)
    this.setData({
      modalName: null,
      classesdata:d
    })
 //   console.log(this.data.classesdata)
  },

  ChooseCheckbox(e) {
  
    let items = this.data.docterlist;
    let values = e.currentTarget.dataset.value;
    this.xuanzhongyishi(values);
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].value == values) {
        items[i].checked = !items[i].checked;
        break
      }
    }
    this.setData({
      docterlist: items
    })
  },

  xuanzhongyishi(valu){
    let cdata=this.data.certificate
    for(let i=0;i<cdata.length;i++){
      if(valu==cdata[i])
      {
        cdata.splice(i,1)
        this.setData({
          certificate:cdata
        })
       return 0
      }
    }
    cdata.push(valu)
    this.setData({
      certificate:cdata
    })
  console.log(cdata)
  },

  ChooseCheckbox1(e) {
    let items = this.data.classeslist;
    let values = e.currentTarget.dataset.value;
    this.xuanzhongyishi1(values);
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].value == values) {
        items[i].checked = !items[i].checked;
        break
      }
    }
    this.setData({
      classeslist: items
    })
  },
  xuanzhongyishi1(valu){
    let cdata=this.data.classes
    for(let i=0;i<cdata.length;i++){
      if(valu==cdata[i])
      {
        cdata.splice(i,1)

        this.setData({
          classes:cdata
        })
        return 0;
      }
    }
    cdata.push(valu)
    this.setData({
      classes:cdata
    })
  console.log(cdata)
  },




  sw1(e){
    this.setData({
      hunf:e.detail.value
    })  
  },
  sw2(e){
    console.log(e.detail.value)
    this.setData({
      chuchai:e.detail.value
    })  
  },
  ///小组号
  grouptext(e){
    let data1=e.detail.value
    let d=this.data.classesdata
    d[this.data.index1].groupnumber=data1
    this.setData({
      classesdata:d
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data.classesdata)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})